﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Handrise.BasicModal.Serialization;

namespace Handrise.CDCable.Business
{
    /// <summary>
    /// 巡视计划服务
    /// </summary>
    public class PlanService
    {
        private ServerExchange serverExchange = new ServerExchange();

        /// <summary>
        /// 下载巡视计划（包含计划、计划设备、缺陷、指挥信息）
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool DownloadPlan(string userName, out byte[] plansBuffer, out byte[] planDevicesBuffer, out byte[] flawsBuffer, out byte[] communicationsBuffer)
        {
            plansBuffer = null;
            planDevicesBuffer = null;
            flawsBuffer = null;
            communicationsBuffer = null;

            //通过主站服务下载计划
            string receiveData = serverExchange.DownloadPlan(userName);

            //处理数据
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(receiveData);
            //先尝试获取正确的返回结果
            XmlNode xnValue = CommonFunction.GetXmlNode(xmlDoc, "message/operation/returnValue/value");
            if (xnValue == null)
            {
                //没有正确的返回结果
                return false;
            }

            //分析返回的内容
            XmlDocument userInfoXmlDoc = new XmlDocument();
            userInfoXmlDoc.LoadXml(xnValue.InnerText);
            XmlNode xnEnitys = CommonFunction.GetXmlNode(userInfoXmlDoc, "PatrolsDownLoads");
            if (xnEnitys == null)
                return false;

            CompactFormatterPlus cfp = new CompactFormatterPlus();

            //获取巡视计划
            List<Object> plans = CommonFunction.GetEntitysFromXML(xnEnitys, typeof(Plan));
            //设置计划数据
            foreach (Plan plan in plans)  
            {
                //设置计划的所属人
                plan.UserName = userName;

            }
            plansBuffer = cfp.Serialize(plans);

            //获取巡视设备
            List<Object> planDevices = CommonFunction.GetEntitysFromXML(xnEnitys, typeof(PlanDevice));
            //设置巡视设备数据
            foreach (PlanDevice planDevice in planDevices)
            {
                //设置巡视设备所属人
                planDevice.UserName = userName;
            }
            planDevicesBuffer = cfp.Serialize(planDevices);

            //获取缺陷信息
            List<Object> flaws = CommonFunction.GetEntitysFromXML(xnEnitys, typeof(Flaw));
            //设置巡视设备数据
            foreach (Flaw flaw in flaws)
            {
                //设置缺陷信息
                flaw.Source = "Server";
                flaw.UserName = userName;
                flaw.Title = flaw.Part + flaw.Phenomenon;
            }
            flawsBuffer = cfp.Serialize(flaws);

            //指挥信息
            List<Object> communications = CommonFunction.GetEntitysFromXML(xnEnitys, typeof(Communication));
            communicationsBuffer = cfp.Serialize(communications);

            return true;

        }

        /// <summary>
        /// 确认下载巡视计划完成
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="planIDs">下载的巡视计划ID，多个通过“,”分隔</param>
        /// <param name="communicationIDs">下载的指挥信息，多个通过“,”分隔</param>
        /// <returns></returns>
        public bool ComfireDownloadPlan(string userName, string planIDs, string communicationIDs)
        {
            //通过主站服务确认下载完成
            string receiveData = serverExchange.ComfireDownloadPlan(userName, planIDs, communicationIDs);

            return CommonFunction.ReturnDataIsOk(receiveData); ;
        }

        /// <summary>
        /// 上传计划状态
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="plansBuffer">计划实体序列化流</param>
        /// <returns></returns>
        public bool UploadPlanState(string userName,byte[] plansBuffer)
        {
            //获取计划实体
            CompactFormatterPlus cfp = new CompactFormatterPlus();
            List<Plan> plans = (List<Plan>)cfp.Deserialize(plansBuffer);

            foreach (Plan plan in plans)
            {
                //通过主站服务上传任务状态
                string receiveData = serverExchange.UploadPlanState(userName,plan.PlanID, plan.State);                
            }

            return true;

        }
    }
}
